001 /*
002 * Copyright 2006 Stephen McConnell
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
013 * implied.
014 *
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package net.dpml.tools.tasks;
020
021 import java.io.File;
022
023 import net.dpml.library.info.Scope;
024
025 import net.dpml.tools.Context;
026
027 import org.apache.tools.ant.BuildException;
028 import org.apache.tools.ant.Project;
029 import org.apache.tools.ant.taskdefs.Rmic;
030 import org.apache.tools.ant.types.Path;
031
032 /**
033 * Compile sources located in ${project.target}/main to java class file under
034 * the ${project.target}/classes directory. Properties influencing the compilation
035 * include:
036 * <ul>
037 * <li>project.javac.debug : boolean true (default) or false</li>
038 * <li>project.javac.fork: boolean true or false (default) </li>
039 * <li>project.javac.deprecation: boolean true (default) or false</li>
040 * </ul>
041 * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
042 * @version 1.1.1
043 */
044 public class RMICTask extends GenericTask
045 {
046 private final Context m_context;
047 private String[] m_includes = new String[0];
048 private String[] m_excludes = new String[0];
049 private String m_classPathRef;
050 private File m_base;
051
052 /**
053 * Creation of a new RMICTask.
054 * @param context the project context
055 */
056 public RMICTask( Context context )
057 {
058 super();
059 m_context = context;
060 try
061 {
062 setProject( context.getProject() );
063 setTaskName( "rmic" );
064 setBase( context.getTargetClassesMainDirectory() );
065 context.getPath( Scope.RUNTIME );
066 setClasspathRef( "project.compile.path" );
067 }
068 catch( Exception e )
069 {
070 throw new BuildException( e );
071 }
072 }
073
074 /**
075 * Set the id of the compilation classpath.
076 * @param id the classpath reference
077 */
078 public void setClasspathRef( String id )
079 {
080 m_classPathRef = id;
081 }
082
083 /**
084 * Set the base directory.
085 * @param base the base directory
086 */
087 public void setBase( File base )
088 {
089 m_base = base;
090 }
091
092 /**
093 * Set the includes.
094 * @param includes the include paths
095 */
096 public void setIncludes( String[] includes )
097 {
098 m_includes = includes;
099 }
100
101 /**
102 * Set the excludes.
103 * @param excludes the excluded paths
104 */
105 public void setExcludes( String[] excludes )
106 {
107 m_excludes = excludes;
108 }
109
110 /**
111 * Task execution.
112 */
113 public void execute()
114 {
115 if( null == m_base )
116 {
117 final String error =
118 "Missing 'base' argument.";
119 throw new BuildException( error, getLocation() );
120 }
121
122 if( !m_base.exists() )
123 {
124 return;
125 }
126
127 final Rmic rmic = (Rmic) getProject().createTask( "rmic" );
128 rmic.setTaskName( "rmic" );
129 final Project project = m_context.getProject();
130 rmic.setProject( project );
131 rmic.setBase( m_base );
132 final Path classpath = getClasspath();
133 rmic.setClasspath( classpath );
134
135 // populate includes/excludes
136
137 for( int i=0; i<m_includes.length; i++ )
138 {
139 String include = m_includes[i];
140 rmic.createInclude().setName( include );
141 }
142
143 for( int i=0; i<m_excludes.length; i++ )
144 {
145 String exclude = m_excludes[i];
146 rmic.createExclude().setName( exclude );
147 }
148
149 // initialize and execute
150
151 rmic.init();
152 rmic.execute();
153 }
154
155 private Path getClasspath()
156 {
157 if( null != m_classPathRef )
158 {
159 return (Path) getProject().getReference( m_classPathRef );
160 }
161 else
162 {
163 final String error =
164 "Missing 'classpathRef' attribute.";
165 throw new BuildException( error, getLocation() );
166 }
167 }
168 }